<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="入住信息编码" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入入住信息编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户名称" prop="itemId">
        <el-select v-model="queryParams.clientId" placeholder="选择客户名称" style="width: 100%;" clearable>
          <el-option v-for="item in clientInfoList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="客房名称" prop="itemId">
        <el-select v-model="queryParams.roomId" placeholder="选择客房名称" style="width: 100%;" clearable>
          <el-option v-for="item in roomInfoList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入联系方式" clearable />
      </el-form-item>
      <el-form-item label="入住时间">
        <el-date-picker v-model="daterangeCheckinTime" style="width: 215px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="退房时间">
        <el-date-picker v-model="daterangeCheckoutTime" style="width: 215px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:info:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="checkin">
          入住办理</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:info:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:info:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:info:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="50">
        <template slot-scope="scop">
          {{ scop.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="入住信息编码" align="center" prop="code" />
      <el-table-column label="房间" align="center" prop="roomName" />
      <el-table-column label="房间状态" align="center" prop="roomType">                
        <template slot-scope="scope">
          <dict-tag :options="dict.type.room_status" :value="scope.row.roomType"/>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center" prop="clientName" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="入住时间" align="center" prop="checkinTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkinTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退房时间" align="center" prop="checkoutTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkoutTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="支付内容" align="center" prop="name" />
      <el-table-column label="支付宝订单号" align="center" prop="ali_no" />
      <el-table-column label="支付状态" align="center" prop="state" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:info:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleCheckOut(scope.row)"
          :disabled="scope.row.roomType === 'CHECKED_OUT'"
            >退房</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改入住信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!-- <el-form-item label="入住信息编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入入住信息编码" />
        </el-form-item> -->
        <el-form-item label="客户名称" prop="clientId">
          <el-select v-model="form.clientId" placeholder="选择客户名称" style="width: 100%;" clearable>
            <el-option v-for="item in clientInfoList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客房名称" prop="roomId">
          <el-select v-model="form.roomId" placeholder="选择客房名称" style="width: 100%;" clearable>
            <el-option v-for="item in addRoomInfoList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="入住时间" prop="checkinTime">
          <el-date-picker clearable v-model="form.checkinTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择入住时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退房时间" prop="checkoutTime">
          <el-date-picker clearable v-model="form.checkoutTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择退房时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 入住办理对话框 -->
    <el-dialog :title="title" :visible.sync="checkInShow" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="身份证号" prop="userCode">
          <el-input v-model="form.userCode" placeholder="请输入身份证号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="checkInShowsubmitForm">确 定</el-button>
        <el-button @click="checkInShowcancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 入住人信息对话框 -->
    <el-dialog :title="title" :visible.sync="clientinfoShow" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="客户名称">
          <el-input v-model="form.name" disabled placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="form.code" disabled placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="用户性别">
          <el-select v-model="form.sex" disabled placeholder="请选择性别" style="width: 100%;">
            <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="clientinfoShowsubmitForm">确 定</el-button>
        <el-button @click="clientinfoShowcancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, checkInByUserCode, CheckOut } from "@/api/pms/pmsCheckinInfo";
import { roomInfoList } from "@/api/pms/pmsGuestroomInfo";
import { ClientList, getInfoByCode } from "@/api/pms/pmsClientInfo";

export default {
  name: "pmsCheckinInfo",
  dicts: ['sys_user_sex','room_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入住信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否入住办理弹出层
      checkInShow: false,
      clientinfoShow: false,
      // 备注时间范围
      daterangeCheckinTime: [],
      // 备注时间范围
      daterangeCheckoutTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        roomId: null,
        clientId: null,
        checkinTime: null,
        checkoutTime: null,
        phone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        clientId: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        roomId: [
          { required: true, message: "客房不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "联系方式不能为空", trigger: "blur" }
        ],
        checkinTime: [
          { required: true, message: "入住时间不能为空", trigger: "blur" }
        ],
        checkoutTime: [
          { required: true, message: "退房时间不能为空", trigger: "blur" }
        ],
        userCode: [
          { required: true, message: "身份证号码不能为空", trigger: "blur" }
        ],
      },
      // 客户信息List
      clientInfoList: [],
      // 客房信息List
      roomInfoList: [],
      addRoomInfoList: []
    };
  },
  created() {
    this.getList();
    this.getClientInfoList();
    this.getRoomInfoList();
    this.getAddRoomInfoList();
  },
  methods: {
    /** 查询入住信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCheckinTime && '' != this.daterangeCheckinTime) {
        this.queryParams.params["beginCheckinTime"] = this.daterangeCheckinTime[0];
        this.queryParams.params["endCheckinTime"] = this.daterangeCheckinTime[1];
      }
      if (null != this.daterangeCheckoutTime && '' != this.daterangeCheckoutTime) {
        this.queryParams.params["beginCheckoutTime"] = this.daterangeCheckoutTime[0];
        this.queryParams.params["endCheckoutTime"] = this.daterangeCheckoutTime[1];
      }
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 入住办理取消按钮
    checkInShowcancel() {
      this.checkInShow = false;
      this.reset();
    },
    // 取消客户信息按钮
    clientinfoShowcancel() {
      this.clientinfoShow = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        roomId: null,
        clientId: null,
        checkinTime: null,
        checkoutTime: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        userCode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCheckinTime = [];
      this.daterangeCheckoutTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加入住信息";
    },
    /** 入住按钮操作 */
    checkin() {
      this.reset();
      this.checkInShow = true;
      this.title = "入住办理";
    },
    /** 客户信息 */
    clientinfo() {
      this.clientinfoShow = true;
      this.title = "客户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改入住信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 入住办理提交按钮 */
    checkInShowsubmitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          getInfoByCode(this.form.userCode).then(response => {
            if (response.code !== '500') {
              this.form = response.data;
              this.checkInShow = false;
              this.clientinfo();
            } else {
              this.checkInShow = false;
              this.getList();
              this.reset();
            }
          });
        }
      });
    },

    /** 客户信息提交按钮 */
    clientinfoShowsubmitForm() {
      checkInByUserCode(this.form.id).then(response => {
        this.clientinfoShow = false;
        this.$modal.msgSuccess("客户入住成功");
        this.reset();
        this.getList();
      });
    },
    /** 退房按钮操作 */
    handleCheckOut(row) {
      const id = row.id
      this.$modal.confirm('是否确认退房？').then(function () {
        return CheckOut(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("退房成功");
      }).catch(() => { });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function () {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    /** 查询客户信息下拉 */
    getClientInfoList() {
      ClientList().then(response => {
        this.clientInfoList = response;
      });
    },
    /** 查询房间信息下拉 */
    getRoomInfoList() {
      roomInfoList().then(response => {
        this.roomInfoList = response;
      });
    },    /** 查询房间信息下拉 */
    getAddRoomInfoList() {
      roomInfoList({ type: 'LEISURE' }).then(response => {
        this.addRoomInfoList = response;
      });
    },
  }
};
</script>
